<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('训练包选择')"/>
    <th:block th:include="include :: select2-css"/>
    <style>
        .popover-show {
            cursor: pointer;
        }
    </style>
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-plan-add">
        <input id="childCourseId" name="childCourseId" th:value="*{childCourseId}" hidden="true">
        <input id="ageGrade" name="childCourseId" th:value="*{ageGrade}" hidden="true">

        <div class="form-group">
            <label class="col-sm-2 control-label is-required">学员：</label>
            <div class="col-sm-6">
                <select id="childId" name="childId" class="form-control" onchange="loadLinkProject()">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label is-required">项目：</label>
            <div class="col-sm-6">
                <select id="linkProject" name="linkProject" class="form-control" onchange="linkProjectChange()">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="form-group" id="linkProject5" style="display: none">
            <label class="col-sm-2 control-label is-required">项目2：</label>
            <div class="col-sm-6">
                <select id="linkProject1" name="linkProject1" class="form-control">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label is-required">环节：</label>
            <div class="col-sm-6">
                <select id="linkNo" name="linkNo" class="form-control" onchange="linkNoChange()">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: select2-js"/>
<script th:inline="javascript">
    var projectData = {

        "ageGrade1": [
            {
                "projectCode": "S0201",
                "projectName": "排队回家",
                "linkNo": [2, 3],
                "childList": []
            },
            {
                "projectCode": "S0512",
                "projectName": "蹦床",
                "linkNo": [4, 7],
                "childList": []
            },
            {
                "projectCode": "",
                "projectName": "旋转项目",
                "linkNo": [4, 7],
                "childList": [
                    {
                        "projectCode": "S1204",
                        "projectName": "大陀螺"
                    },
                    {
                        "projectCode": "S1206",
                        "projectName": "小滑车旋转"
                    },
                    {
                        "projectCode": "S1202",
                        "projectName": "竖抱筒"
                    },
                    {
                        "projectCode": "S1203",
                        "projectName": "网缆"
                    },
                    {
                        "projectCode": "S1207",
                        "projectName": "旋转平衡台"
                    },
                    {
                        "projectCode": "S1209",
                        "projectName": "其他"
                    }
                ]
            },
            {
                "projectCode": "",
                "projectName": "手眼协调项目",
                "linkNo": [4, 7],
                "childList": [
                    {
                        "projectCode": "S1403",
                        "projectName": "88轨道"
                    },
                    {
                        "projectCode": "S1401",
                        "projectName": "串珠子"
                    },
                    {
                        "projectCode": "S1402",
                        "projectName": "夹珠子"
                    },
                    {
                        "projectCode": "S1404",
                        "projectName": "搭积木"
                    },
                    {
                        "projectCode": "S1405",
                        "projectName": "其他"
                    }
                ]
            },
            {
                "projectCode": "",
                "projectName": "游戏",
                "linkNo": [4, 7],
                "childList": [
                    {
                        "projectCode": "S1717",
                        "projectName": "其他"
                    }
                ]
            }

        ],
        "ageGrade2": [
            {
                "projectCode": "S1208",
                "projectName": "摸墙转",
                "linkNo": [2, 3],
                "childList": []
            },
            {
                "projectCode": "S0512",
                "projectName": "蹦床",
                "linkNo": [4, 7],
                "childList": []
            },
            {
                "projectCode": "S0602",
                "projectName": "独脚椅",
                "linkNo": [4, 7],
                "childList": []
            },
            {
                "projectCode": "",
                "projectName": "旋转项目",
                "linkNo": [4, 7],
                "childList": [
                    {
                        "projectCode": "S1204",
                        "projectName": "大陀螺"
                    },
                    {
                        "projectCode": "S1206",
                        "projectName": "小滑车旋转"
                    },
                    {
                        "projectCode": "S1202",
                        "projectName": "竖抱筒"
                    },
                    {
                        "projectCode": "S1203",
                        "projectName": "网缆"
                    },
                    {
                        "projectCode": "S1207",
                        "projectName": "旋转平衡台"
                    },
                    {
                        "projectCode": "S1209",
                        "projectName": "其他"
                    }
                ]
            },
            {
                "projectCode": "",
                "projectName": "手眼协调项目",
                "linkNo": [4, 7],
                "childList": [
                    {
                        "projectCode": "S1403",
                        "projectName": "88轨道"
                    },
                    {
                        "projectCode": "S1401",
                        "projectName": "串珠子"
                    },
                    {
                        "projectCode": "S1402",
                        "projectName": "夹珠子"
                    }
                ]
            },
            {
                "projectCode": "",
                "projectName": "游戏",
                "linkNo": [8],
                "childList": [
                    {
                        "projectCode": "S1717",
                        "projectName": "其他"
                    }
                ]
            }

        ],
        "ageGrade3": [
            {
                "projectCode": "S1208",
                "projectName": "摸墙转",
                "linkNo": [2, 3],
                "childList": []
            },
            {
                "projectCode": "S0512",
                "projectName": "蹦床",
                "linkNo": [4, 7],
                "childList": []
            },
            {
                "projectCode": "",
                "projectName": "旋转项目",
                "linkNo": [4, 7],
                "childList": [
                    {
                        "projectCode": "S1204",
                        "projectName": "大陀螺"
                    },
                    {
                        "projectCode": "S1206",
                        "projectName": "小滑车旋转"
                    },
                    {
                        "projectCode": "S1202",
                        "projectName": "竖抱筒"
                    },
                    {
                        "projectCode": "S1203",
                        "projectName": "网缆"
                    },
                    {
                        "projectCode": "S1207",
                        "projectName": "旋转平衡台"
                    },
                    {
                        "projectCode": "S120303",
                        "projectName": "网缆+插棍"
                    }
                ]
            },
            {
                "projectCode": "",
                "projectName": "手眼协调项目",
                "linkNo": [4, 7],
                "childList": [
                    {
                        "projectCode": "S1403",
                        "projectName": "88轨道"
                    },
                    {
                        "projectCode": "S0811",
                        "projectName": "对墙打乒乓球"
                    },
                    {
                        "projectCode": "S1402",
                        "projectName": "夹珠子"
                    }
                ]
            },
            {
                "projectCode": "",
                "projectName": "游戏",
                "linkNo": [8],
                "childList": [
                    {
                        "projectCode": "S1717",
                        "projectName": "其他"
                    }
                ]
            }

        ],
        "ageGrade4": [
            {
                "projectCode": "S1208",
                "projectName": "摸墙转",
                "linkNo": [2, 3],
                "childList": []
            },
            {
                "projectCode": "S0512",
                "projectName": "蹦床",
                "linkNo": [4, 7],
                "childList": []
            },
            {
                "projectCode": "",
                "projectName": "旋转项目",
                "linkNo": [4, 7],
                "childList": [
                    {
                        "projectCode": "S1206",
                        "projectName": "小滑车旋转"
                    },
                    {
                        "projectCode": "S1202",
                        "projectName": "竖抱筒"
                    },
                    {
                        "projectCode": "S1203",
                        "projectName": "网缆"
                    },
                    {
                        "projectCode": "S1207",
                        "projectName": "旋转平衡台"
                    },
                    {
                        "projectCode": "S120303",
                        "projectName": "网缆+插棍"
                    }
                ]
            },
            {
                "projectCode": "",
                "projectName": "手眼协调项目",
                "linkNo": [4, 7],
                "childList": [
                    {
                        "projectCode": "S1403",
                        "projectName": "88轨道"
                    },
                    {
                        "projectCode": "S0811",
                        "projectName": "对墙打乒乓球"
                    }
                ]
            },
            {
                "projectCode": "",
                "projectName": "游戏",
                "linkNo": [8],
                "childList": [
                    {
                        "projectCode": "S1717",
                        "projectName": "其他"
                    }
                ]
            }

        ],
        "ageGrade5": [
            {
                "projectCode": "S1208",
                "projectName": "摸墙转",
                "linkNo": [2, 3],
                "childList": []
            },
            {
                "projectCode": "S0512",
                "projectName": "蹦床",
                "linkNo": [4, 7],
                "childList": []
            },
            {
                "projectCode": "",
                "projectName": "旋转项目",
                "linkNo": [4, 7],
                "childList": [
                    {
                        "projectCode": "S1205",
                        "projectName": "大陀螺"
                    },
                    {
                        "projectCode": "S1206",
                        "projectName": "小滑车旋转"
                    },
                    {
                        "projectCode": "S1202",
                        "projectName": "竖抱筒"
                    },
                    {
                        "projectCode": "S1203",
                        "projectName": "网缆"
                    },
                    {
                        "projectCode": "S1207",
                        "projectName": "旋转平衡台"
                    },
                    {
                        "projectCode": "S120303",
                        "projectName": "网缆+插棍"
                    }
                ]
            },
            {
                "projectCode": "",
                "projectName": "手眼协调项目",
                "linkNo": [4, 7],
                "childList": [
                    {
                        "projectCode": "S1402",
                        "projectName": "夹珠子"
                    },
                    {
                        "projectCode": "S1403",
                        "projectName": "88轨道"
                    },
                    {
                        "projectCode": "S0811",
                        "projectName": "对墙打乒乓球"
                    }
                ]
            },
            {
                "projectCode": "",
                "projectName": "游戏",
                "linkNo": [8],
                "childList": [
                    {
                        "projectCode": "S1717",
                        "projectName": "其他"
                    }
                ]
            }

        ]
    };


</script>
<script th:inline="javascript">
    // 权限

    // 字典

    var prefix = ctx + "business/prepareClass";
    var i = 0;
    $(function () {
        //加载本节课学员
        $.ajax({
            url: prefix + "/listCourseChild?childCourseId=" + $("#childCourseId").val(),
            type: "post",
            success: function (result) {
                if (result.data.length > 0) {
                    var html = "";
                    for (var i in result.data) {
                        var child = result.data[i];
                        html += "<option value=\"" + child.id + "\">" + child.name + "</option>";
                    }
                    $("#childId").append(html);
                }
            }
        });
         // loadLinkProject();
    });

    function loadLinkProject() {
        $("#linkProject").empty();
        var ageGrade = $("#ageGrade").val();
        var projectList = [];
        if (ageGrade == 1) {
            projectList = projectData.ageGrade1;
        }
        if (ageGrade == 2) {
            projectList = projectData.ageGrade2;
        }
        if (ageGrade == 3) {
            projectList = projectData.ageGrade3;
        }
        if (ageGrade == 4) {
            projectList = projectData.ageGrade4;
        }
        if (ageGrade == 5) {
            projectList = projectData.ageGrade5;
        }
        if (ageGrade == 6) {
            projectList = projectData.ageGrade5;
        }
        var html = "";
        html += "<option value=\"\" >请选择</option>";
        var projectCodes=[];
        for (var i in projectList) {
            var json = projectList[i];
            if (json.childList.length > 0) {
                for (var j in json.childList) {
                    projectCodes.push(json.childList[j].projectCode);
                }
            } else {
                projectCodes.push(json.projectCode);
            }
        }
        //获取各项目训练次数
        var getProjectCompleteTimesReq={
            "childId":$("#childId").val(),
            "projectCodes":projectCodes
        };
        var projectTimes={};
        $.ajax({
            url: prefix+"/getProjectCompleteTimes",
            type: "post",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify(getProjectCompleteTimesReq),
            async:false,
            success: function (result) {
                projectTimes=result.data
            },error: function (request) {
                $.modal.alertError("系统错误");
            }
        });

        for (var i in projectList) {
            var json = projectList[i];
            if (json.childList.length > 0) {
                html += '<optgroup label="--' + json.projectName + '--">';
                for (var j in json.childList) {
                    var times="("+projectTimes[json.childList[j].projectCode]+"次)";
                    html += "<option value=\"" + json.childList[j].projectCode + "\" >" + json.childList[j].projectName+times + "</option>";
                }
                html += '</optgroup>';
            } else {
                var times="("+projectTimes[json.projectCode]+"次)";
                html += "<option value=\"" + json.projectCode + "\" >" + json.projectName +times+ "</option>";
            }
        }
        $("#linkProject").append(html);

    }

    function loadLinkProject1() {
        $("#linkProject1").empty();
        var ageGrade = $("#ageGrade").val();
        var projectList = [];
        if (ageGrade == 1) {
            projectList = projectData.ageGrade1;
        }
        if (ageGrade == 2) {
            projectList = projectData.ageGrade2;
        }
        if (ageGrade == 3) {
            projectList = projectData.ageGrade3;
        }
        if (ageGrade == 4) {
            projectList = projectData.ageGrade4;
        }
        if (ageGrade == 5) {
            projectList = projectData.ageGrade5;
        }
        if (ageGrade == 6) {
            projectList = projectData.ageGrade5;
        }
        var html = "";
        html += "<option value=\"\" >请选择</option>";
        var projectCodes=[];
        for (var i in projectList) {
            var json = projectList[i];
            if (json.childList.length > 0) {
                for (var j in json.childList) {
                    projectCodes.push(json.childList[j].projectCode);
                }
            } else {
                projectCodes.push(json.projectCode);
            }
        }
        //获取各项目训练次数
        var getProjectCompleteTimesReq={
            "childId":$("#childId").val(),
            "projectCodes":projectCodes
        };
        var projectTimes={};
        $.ajax({
            url: prefix+"/getProjectCompleteTimes",
            type: "post",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify(getProjectCompleteTimesReq),
            async:false,
            success: function (result) {
                projectTimes=result.data
            },error: function (request) {
                $.modal.alertError("系统错误");
            }
        });

        for (var i in projectList) {
            var json = projectList[i];
            if(json.linkNo.indexOf(4)<=-1 &&json.linkNo.indexOf(7)<=-1){
                continue;
            }
            if (json.childList.length > 0) {
                html += '<optgroup label="--' + json.projectName + '--">';
                for (var j in json.childList) {
                    var times="("+projectTimes[json.childList[j].projectCode]+"次)";
                    html += "<option value=\"" + json.childList[j].projectCode + "\" >" + json.childList[j].projectName +times+ "</option>";
                }
                html += '</optgroup>';
            } else {
                var times="("+projectTimes[json.projectCode]+"次)";
                html += "<option value=\"" + json.projectCode + "\" >" + json.projectName +times+ "</option>";
            }
        }
        $("#linkProject1").append(html);

    }
    // 项目选择改变
    function linkProjectChange() {
        var newProject = $("#linkProject").val();
        var childId = $("#childId").val();
        var link23=['S0201','S1208'];
        var link8=['S1717'];
        var html="";
        $("#linkNo").empty();
        html+="<option value=\"\">请选择</option>";
        if(link23.indexOf(newProject)>-1){
            html+="<option value=\"2\">引入2</option>";
            html+="<option value=\"3\">引入3</option>";
        }else if(link8.indexOf(newProject)>-1){
            html+="<option value=\"8\">游戏</option>";
        }else {
            html+="<option value=\"4\">项目1</option>";
            html+="<option value=\"5\">项目2</option>";
            html+="<option value=\"6\">项目3</option>";
            html+="<option value=\"7\">项目4</option>";
        }
        $("#linkNo").append(html);



    };

    function linkNoChange() {
        var childId = $("#childId").val();
        var linkNo = $("#linkNo").val();
        //获取需要替换环节原本项目
        var oldProject="";
        $.ajax({
            url: ctx+"business/courseChildProject/getProject?childCourseId=" + $("#childCourseId").val()+"&childId="+ childId,
            type: "get",
            async:false,
            success: function (result) {
                reqData=result.data;
                switch (linkNo) {
                    case "2":oldProject =result.data.initTwo;break;
                    case "3":oldProject =result.data.initThree;break;
                    case "4":oldProject =result.data.initFour;break;
                    case "5":oldProject =result.data.initFive;break;
                    case "6":oldProject =result.data.initSix;break;
                    case "7":oldProject =result.data.initSeven;break;
                    case "8":oldProject =result.data.initEight;break;
                }
            }
        });
        //获取项目已被替换次数
        $.ajax({
            url: ctx+"business/childProjectConfig/getProjectReplaceTimes?childId=" + childId+"&projectCode="+ oldProject,
            type: "get",
            async:false,
            success: function (result) {
                if(result.data !=undefined){
                    $.modal.msg("【"+result.data.projectName+"】已被替换"+(result.data.replaceTimes==null?0:result.data.replaceTimes)+"次");
                }
            }
        })
        // 当被替换的项目为【大滑板】时需选用两个替换项目
        if(oldProject.indexOf("S0301")>-1){
             $("#linkProject5").css("display","block");
            loadLinkProject1();
        }else {
            $("#linkProject5").css("display","none");
        }

    }

    function submitHandler() {
        var childId = $("#childId").val();
        if (childId == "") {
            $.modal.alertWarning("请选择学员。");
            return;
        }
        var linkNo = $("#linkNo").val();
        if (linkNo == "") {
            $.modal.alertWarning("请选择环节。");
            return;
        }
        var newProject1 = $("#linkProject").val();
        if (newProject1 == "") {
            $.modal.alertWarning("请选择替换项目。");
            return;
        }
        var reqData={};
        var oldProject="";
        //获取需要替换环节原本项目
        $.ajax({
            url: ctx+"business/courseChildProject/getProject?childCourseId=" + $("#childCourseId").val()+"&childId="+ childId,
            type: "get",
            async:false,
            success: function (result) {
                reqData=result.data;
                switch (linkNo) {
                    case "2":oldProject =result.data.initTwo;break;
                    case "3":oldProject =result.data.initThree;break;
                    case "4":oldProject =result.data.initFour;break;
                    case "5":oldProject =result.data.initFive;break;
                    case "6":oldProject =result.data.initSix;break;
                    case "7":oldProject =result.data.initSeven;break;
                    case "8":oldProject =result.data.initEight;break;
                }
            },error: function (request) {
                $.modal.alertError("系统错误");
            }
        });

        var newProject=[];
        newProject.push(newProject1);
        // 当被替换的项目为【大滑板】时需选用两个替换项目
        var newProject2= $("#linkProject1").val();
        if(oldProject.indexOf("S0301")>-1){
            if(newProject1=="" || newProject2==""){
                $.modal.alertWarning("被替换项目为【大滑板】，请选择2个项目。");
                return;
            }else {
                newProject.push(newProject2);
            }
        }
        var replaceRecord=JSON.parse(reqData.replaceRecord);
        var countOldProject="";
        if(replaceRecord==null){
            countOldProject =oldProject;
            replaceRecord=[];
            if(oldProject.indexOf("S0301")>-1){
                var temp={"linkNo":4, "times":1};
                replaceRecord.push(temp);
                temp={"linkNo":5, "times":1};
                replaceRecord.push(temp);
            }else if(newProject1 =="S0201"){
                var temp={"linkNo":2, "times":1};
                replaceRecord.push(temp);
                temp={"linkNo":3, "times":1};
                replaceRecord.push(temp);
            }else {
                var temp={"linkNo":linkNo, "times":1};
                replaceRecord.push(temp);
            }
            reqData.replaceRecord=JSON.stringify(replaceRecord);
        }else {
            var exist=true;
            for(var i in replaceRecord){
                if(replaceRecord[i].linkNo==linkNo){
                    exist=false;
                    replaceRecord[i].times +=1;
                }
            }
            if(exist){
                countOldProject=oldProject;
                // var temp={"linkNo":linkNo, "times":1};
                // replaceRecord.push(temp);
                if(oldProject.indexOf("S0301")>-1){
                    var temp={"linkNo":4, "times":1};
                    replaceRecord.push(temp);
                    temp={"linkNo":5, "times":1};
                    replaceRecord.push(temp);
                }else if(newProject1 =="S0201"){
                    var temp={"linkNo":2, "times":1};
                    replaceRecord.push(temp);
                    temp={"linkNo":3, "times":1};
                    replaceRecord.push(temp);
                }else {
                    var temp={"linkNo":linkNo, "times":1};
                    replaceRecord.push(temp);
                }
            }
            if(replaceRecord.length>3){
                $.modal.alertWarning("本节课可替换的项目数量达到上限，不可再替换。");
                return;
            }else {
                reqData.replaceRecord=JSON.stringify(replaceRecord);
            }
        }

        if(reqData.initOne == oldProject && newProject.length>0){
            reqData.initOne=newProject[0];
            newProject.shift();
        }
        if(reqData.initTwo == oldProject && newProject.length>0){
            reqData.initTwo=newProject[0];
            newProject.shift();
        }
        if(reqData.initThree == oldProject && newProject.length>0){
            reqData.initThree=newProject[0];
            newProject.shift();
        }
        if(reqData.initFour == oldProject && newProject.length>0){
            reqData.initFour=newProject[0];
            newProject.shift();
        }
        if(reqData.initFive == oldProject && newProject.length>0){
            reqData.initFive=newProject[0];
            newProject.shift();
        }
        if(reqData.initSix == oldProject && newProject.length>0){
            reqData.initSix=newProject[0];
            newProject.shift();
        }
        if(reqData.initSeven == oldProject && newProject.length>0){
            reqData.initSeven=newProject[0];
            newProject.shift();
        }
        if(reqData.initEight == oldProject && newProject.length>0){
            reqData.initEight=newProject[0];
            newProject.shift();
        }
        if(reqData.initNine == oldProject && newProject.length>0){
            reqData.initNine=newProject[0];
            newProject.shift();
        }
        //【排队回家】项目为2V1替换，即如果老师选择的替换项目为【排队回家】则要替换掉原环节2、环节3；其余项目为1V1替换。
        if(newProject1 =="S0201"){
            reqData.initTwo=newProject1;
            reqData.initThree=newProject1;
        }
        reqData.countOldProject=countOldProject;
        $.modal.confirm('替换后  不可恢复为原项目  ，请确认是否替换。',function () {
            $.ajax({
                url: prefix + "/replaceInitLink",
                type: "post",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify(reqData),
                success: function (result) {
                    $.operate.successCallback(result);
                    $.table.refresh();
                }
            })

        });

    }

</script>
</body>

</html>